<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class mysqlDb{
    private $con;
    public $error="";
    
    public function __construct($host="localhost",$name="root",$pwd="root",$chart="utf8"){
          $this->connect($host,$name,$pwd);  //调取连接，实例化自动执行
          $this->charset($chart);            //调取编码，实例化自动执行
}
//连接数据库
    public function connect($host="localhost",$name="root",$pwd="root"){
        $this->con=mysqli_connect($host,$name,$pwd);
    }
//选择数据库
    public function selectDb($dbname){
        mysqli_select_db($this->con,$dbname);
    }
//校正编码
    public function charset($chart="utf8"){
        mysqli_set_charset($this->con,$chart);
    }
//查询
    public function select($sql){
        if(empty($sql)) {
            return array();
        }
        $result = mysqli_query($this->con, $sql);
        if(!$result) {
            $this->error = mysqli_error($this->con);
            return array();
        }
        $arr = array();
        while($row = mysqli_fetch_assoc($result)){
            $arr[] = $row;
        }
        return $arr;//返回数据结果
    }
    //添加
        public function add($sql){
           if(empty($sql)) {
            return false;
        }
        $result = mysqli_query($this->con, $sql);
        if(!$result) {
            $this->error = mysqli_error($this->con);
            return false;
        }else{
        return true;
        }  
    } 
  
    //删除
    public function delete($sql){
         if(empty($sql)) {
            return false;
        }
        $result = mysqli_query($this->con, $sql);
        if(!$result) {
            $this->error = mysqli_error($this->con);
            return false;
        }else{
        return 1;
        }  
    }
   // 登录
         public function login($sql) {
        $result = mysqli_query($this->con, $sql);
        if(!$result){
             $this->error = mysqli_error($this->con);
                 return false;
        }
            $row = mysqli_fetch_assoc($result);
//            var_dump($row);
            return $row;
}
    public function __destruct() {
        mysqli_close($this->con);
    }
    
}



